var $,
    form,
    layer;

var user = JSON.parse(localStorage.getItem('user'));
layui.use(['jquery', 'layer', 'form'], function () {
    $ = layui.jquery,
        form = layui.form,
        layer = layui.layer;

    form.on('switch(swi)', function (data){
        var courseId = data.elem.value;
        if (data.elem.checked){
            updateCourseStatus(courseId, 2);
        }else {
            updateCourseStatus(courseId, 3);
        }
    });
});



function updateCourseStatus(courseId, courseStatus) {
    $.ajax({
        url:'/Course/updateCourse',
        data: {
            'courseId': courseId,
            'courseStatus': courseStatus
        },
        dataType: 'json',
        success:function (data) {
            if (data.data) {
                if (courseStatus == 2){
                    layer.msg('上架成功');
                } else if (courseStatus == 3){
                    layer.msg('下架成功');
                }
            }
        }
    })
}

getTableData();
function getTableData(classId, courseName, courseStatus, courseIntro, id, courseNprice){
    $.ajax({
        url:'/Course/getCourseListsByUserId',
        type: 'post',
        dataType: 'json',
        data: {
            'classId': classId,
            'courseName': courseName,
            'courseStatus': courseStatus,
            'courseIntro': courseIntro,
            'id': id,
            'courseNprice': courseNprice,
            'jsUser.userId': user.userId,
            'courseRecord.type' : 2
        },
        success: function (item) {
            getTable(item);
        }
    });
};


//查询表
function getTable(data) {
    layui.use(['jquery', 'layer', 'table'], function () {
        table = layui.table;
        table.render({
            elem: '#demo',
            data: data.data,
            toolbar:'#toolutil',
            page:true,
            limit:5,
            text: {
                none: '暂无相关数据' //默认：无数据。
            },
            cols:[[
                {checkbox:true},
                {field:'id',title:'课程编号',align:'center',height:'20px'},
                {title:'课程图片',align:'center',templet:function(data){
                        return "<img src='"+data.url+"'width='80px' height='80px'/>";
                    }},
                {field:'courseName',title:'课程名称',align:'center'},
                {field:'courseNprice',title:'课程价格',align:'center'},
                {title:'上架/下架',align:'center',templet:function(data){
                        if(data.courseStatus === 2){
                            return"<input type='checkbox' value='"+data.courseId+"' lay-filter='swi' lay-skin='switch' lay-text='上架|下架' checked>";
                        }else if(data.courseStatus === 3){
                            return"<input type='checkbox' value='"+data.courseId+"' lay-filter='swi' lay-skin='switch' lay-text='上架|下架'>";
                        }else{
                            return "";
                        }
                    }},
                {field:'title',title:'课程分类',align:'center', templet:function (data) {
                    return data.courseClass.title;
                    }},
                {field:'courseIntro',title:'课程简介',align:'center'},
                {field:'result',title:'审核状态',align:'center',templet:function(data){
                        var result;
                        if(data.courseRecord.result == 1){
                            result = "审核通过";
                        }else if(data.courseRecord.result == 2){
                            result = "待审核";
                        }else if(data.courseRecord.result == 3){
                            result = "审核驳回";
                        }else{
                            result = "";
                        }
                        return "<div>"+result+"</div>" +
                            "<a class='operation' onclick='auditDetails("+data.courseId+")'>审核详情</a>";
                    }},
                {field:'title',title:'操作',align:'center',templet:function(data){
                        return "<a class='operation' onclick='details("+data.courseId+")'>查看</a>  <a class='operation' onclick='edit("+data.courseId+")'>编辑</a> <br/>"+
                            "<a class='operation' onclick='commodityLog("+data.courseId+")'>日志</a>  <a onclick='delProduct(\""+data.courseId+"\",\""+data.courseCode+"\")' class='operation'>删除</a>";
                    }},
            ]]
        });
    });

};



//修改商品信息
function edit (id) {
    layer.open({
        type: 2,
        offset: 'auto',
        resize: false,
        title: '修改商品',
        maxmin: true,
        area: ['800px', '500px'],
        content: '/course/courseList-layer/courseList-modify.html?tx=' + encodeURI(id)
    });
}

//查看商品详情
function details (id) {
    layer.open({
        type: 2,
        offset: 'auto',
        resize: false,
        title: '商品详情',
        maxmin: true,
        area: ['1200px', '600px'],
        content: '/course/courseList-layer/courseDetails.html?tx=' + encodeURI(id)
    });
}

//添加商品详情
function addProduct () {
    location.href="/course/courseList-add.html";
}

//历史价格日志
function commodityLog (id) {
    layer.open({
        type: 2,
        offset: 'auto',
        resize: false,
        title: '历史价格日志',
        area:['1020px','500px'],
        content: '/course/courseJournal.html?tx=' + encodeURI(id)
    })
}

//根据id删除商品
function delCourse (id) {
    layer.open({
        type: 1,
        offset: 'auto',
        resize: false,
        area:['300px','200px'],
        btn:["确定","取消"],
        content:"<div style='font-size: 20px ; color: red; margin:30px  ;'>是否删除此商品?</div>",
        yes: function (index, layero) {
            $.ajax({
                url: '/Course/deleteCourse',
                dataType: 'json',
                data: {
                    'id': id
                },
                success: function (item) {
                    if (item.data) {
                        layer.msg("删除成功");
                        addProductOperationRecord(4, getDate(), "删除商品信息");
                        getTableData();
                        layer.close(index);
                    } else {
                        layer.msg("删除失败");
                    }
                }
            });
        },
        btn2: function (index, layero) {
            layer.close(index);
        }
    });
}

//高级检索
function accurate () {
    layer.open({
        type: 2,
        resize: false,
        offset: 'auto',
        title: '高级检索',
        area: ['600px', '500px'],
        content: '/course/courseList-layer/courseList-precisesearch.html'
    });
};

// 添加的操作日志
function addProductOperationRecord(operationType, executionTime, remarks) {
    $.ajax({
        url:'/Product/addProductOperationRecord',
        dataType:'json',
        data:{
            "operationType": operationType,
            "executionTime": executionTime,
            "remarks": remarks,
            "updateId": user.userId,
            "operationList": 'js_course',
            "type": 2
        }
    });
};

//搜索
function selProduct(){
    var val = form.val('formTest');
    getTableData(val.classId, val.courseName, val.courseStatus, val.courseIntro);
};

//审核详情
function auditDetails (id) {
    layer.open({
        type: 2,
        offset: 'auto',
        title: '审核详情',
        resize: false,
        area:['1020px','500px'],
        content: '/course/courseList-layer/course-auditDetails.html?tx=' + encodeURI(id)
    });
}

//获取时间
function getDate() {
    var date = new Date().toLocaleString('zh', { hour12: false }).split('/').join('-');
    var temp = date.substring(0, 9);
    date = date.substr(date.lastIndexOf('-') + 3);
    date = temp + date;
    return date;
}


//刷新页面
function refresh (){
    location.reload();
}

//返回首页
function backOff(){
    parent.jumpSystemHome();
}
